package com.thread;

public class SyncDemo2 {
    public static void main(String[] args) {
        Shop shop = new Shop();
        Thread t1 = new Thread(() -> shop.buy(), "zs");
        Thread t2 = new Thread(() -> shop.buy(), "ls");
        t1.start();
        t2.start();

    }
}

class Shop {
    public synchronized void buy() {
        try {
            Thread thread = Thread.currentThread();
            System.out.println(thread.getName() + "正在挑衣服");
            Thread.sleep(5000);
            System.out.println(thread.getName() + "正在试衣服");
            Thread.sleep(5000);
            System.out.println(thread.getName() + "买好了");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
